# This file contains the fastlane.tools configuration
# You can find the documentation at https://docs.fastlane.tools
#
# For a list of all available actions, check out
#
#     https://docs.fastlane.tools/actions
#
# For a list of all available plugins, check out
#
#     https://docs.fastlane.tools/plugins/available-plugins
#

# Uncomment the line if you want fastlane to automatically update itself
# update_fastlane

# This is the minimum version number required.
# Update this, if you use features of a newer version
fastlane_version "2.3.1"

fastlane_require 'dotenv'
Dotenv.load '.env.secret'

default_platform :mac

platform :mac do
  lane :release do |options|
    build = "build"
    scheme = "ONLYOFFICE"
    app = "#{build}/#{scheme}.app"

    desc 'Cleanup'
    sh("rm -rf ../#{build}")

    desc 'Get the version from plist'
    version_number = get_info_plist_value(
      path: "ONLYOFFICE/Resources/#{scheme}/Info.plist",
      key: "CFBundleShortVersionString"
    )
    build_number = get_info_plist_value(
      path: "ONLYOFFICE/Resources/#{scheme}/Info.plist",
      key: "CFBundleVersion"
    )

    desc 'Build for developer id and notarize'
    gym(
      scheme: scheme,
      configuration: 'Release',
      clean: true,
      output_directory: build,
      codesigning_identity: ENV["CODESIGNING_IDENTITY"],
      export_method: 'developer-id',
      skip_package_pkg: true
    )

    notarize(
      package: app,
      print_log: true,
      verbose: true
    )

    desc 'Prepare zip for delta update'
    sh("ditto -c -k --rsrc --keepParent #{ENV['PWD']}/#{app} #{ENV['PWD']}/#{build}/#{scheme}-#{version_number}.zip")

    desc 'Create DMG image'
    sh("npm install appdmg")
    sh("npx appdmg resources/appdmg.json ../#{build}/#{scheme}.dmg")

    if options[:skip_git_bump]
      next
    end

    desc 'Creates a bump version commit'
    commit_version_bump(
      message: "[macos] Version bumped to v#{version_number}(#{build_number})",
      force: true
    )

    desc 'Create a local tag with the new version'
    add_git_tag(
      tag: "macos/v#{version_number}-#{build_number}",
      build_number: build_number
    )

    # push changes
    push_to_git_remote
  end

  # x86 only
  lane :release_x86 do |options|
    build = "build"
    scheme = "ONLYOFFICE-x86"
    appName = "ONLYOFFICE"
    app = "#{build}/#{appName}.app"

    desc 'Cleanup'
    sh("rm -rf ../#{build}")

    desc 'Get the version from plist'
    version_number = get_info_plist_value(
      path: "ONLYOFFICE/Resources/#{scheme}/Info.plist",
      key: "CFBundleShortVersionString"
    )
    build_number = get_info_plist_value(
      path: "ONLYOFFICE/Resources/#{scheme}/Info.plist",
      key: "CFBundleVersion"
    )

    desc 'Build for developer id and notarize'
    gym(
      scheme: scheme,
      configuration: 'Release',
      clean: true,
      output_directory: build,
      codesigning_identity: ENV["CODESIGNING_IDENTITY"],
      export_method: 'developer-id',
      skip_package_pkg: true
    )

    notarize(
      package: app,
      print_log: true,
      verbose: true
    )

    desc 'Prepare zip for delta update'
    sh("ditto -c -k --rsrc --keepParent #{ENV['PWD']}/#{app} #{ENV['PWD']}/#{build}/#{scheme}-#{version_number}.zip")

    desc 'Create DMG image'
    sh("npm install appdmg")
    sh("npx appdmg resources/appdmg.json ../#{build}/#{appName}.dmg")

    if options[:skip_git_bump]
      next
    end

    desc 'Creates a bump version commit'
    commit_version_bump(
      message: "[macos] Version bumped to v#{version_number}-x86(#{build_number})",
      force: true
    )

    desc 'Create a local tag with the new version'
    add_git_tag(
      tag: "macos/v#{version_number}-#{build_number}-x86",
      build_number: build_number
    )

    # push changes
    push_to_git_remote
  end

end